﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using Model;
using DALFAC;
using DAL;
using IDAL;

namespace BLL
{
    public class UserBLL: IUserBLL
    {
        //获取推荐用户
        public int Login(string username, string password)
        {
            int validator = -1;
            DAL.UserLoginDAL userLogin = (DAL.UserLoginDAL)DALFAC.DataAccess.CreateUserLogin();
            if (userLogin != null)
            {
                if (userLogin.userNameExists(username))
                {
                    Model.UserLogin user = (Model.UserLogin)userLogin.getUserByUsername(username);
                    string psd = user.getUL_password();
                    if (psd.Equals(password))
                    {
                        if (0 == user.getUL_status())
                        {
                            
                            user.setUL_lastLoginTime(DateTime.Now);
                            userLogin.Update(user);
                            //正常登陆
                            validator = 0;

                        }
                        else {
                            //用户受限
                            validator = 3;
                        }
                    }
                    else {
                        //密码错误
                        validator = 2;
                    }
                }
                else {
                    //用户名不存在
                    validator = 1;
                }
            }

            return validator;

        }

        //获取推荐相册
        public bool register(Model.UserInfo newUser)
        {
            bool register = false;
            string UL_userName = newUser.getUI_userName();
            string UI_password = newUser.getUI_password();           
            System.DateTime UL_lastLoginTime = System.DateTime.Now;
            int UL_status = 0;

            DAL.UserInfoDAL user = (DAL.UserInfoDAL)DALFAC.DataAccess.CreateUserInfo();
            DAL.UserLoginDAL login = (DAL.UserLoginDAL)DALFAC.DataAccess.CreateUserLogin();
            DAL.AccountsDAL accountDal = (DAL.AccountsDAL)DALFAC.DataAccess.CreateAccounts();

            if (null != user)
            {
                if (!user.userExistByName(newUser.getUI_userName()))
                {
                    if (user.Add(newUser))
                    {
                        if (null != accountDal)
                        {
                            Model.Accounts accountModel = new Model.Accounts();
                            int userId = user.userNameUserInfo(newUser.getUI_userName()).getUI_userId();
                            DateTime timeout = DateTime.Now.AddYears(2);
                            accountModel.setAC_userId(userId);
                            accountModel.setAC_balance(0);
                            accountModel.setAC_timeOut(timeout);

                            if (accountDal.Add(accountModel))
                            {
                                if (null != login)
                                {
                                    register = login.Add(UL_userName, UI_password, UL_lastLoginTime, UL_status);
                                    if (!register)
                                    {
                                        user.UserDeleteByName(newUser.getUI_userName());
                                    }   
                                }else{
                                    user.UserDeleteByName(newUser.getUI_userName());
                                }
                            }
                        }
                    }
                }
            }
            return register;
        }

        //获取推荐照片
        public bool modifyUser(Model.UserInfo modUser)
        {
            bool modifier = false;
            DAL.UserInfoDAL userInfoDal = (DAL.UserInfoDAL)DALFAC.DataAccess.CreateUserInfo();
            DAL.UserLoginDAL userLoginDal = (DAL.UserLoginDAL)DALFAC.DataAccess.CreateUserLogin();
          

                Model.UserInfo userInfo = (Model.UserInfo)userInfoDal.userIdUserInfo(modUser.getUI_userId());
                if (null != userInfo)
                {
                    string newPsd = modUser.getUI_password();
                    string psd = userInfo.getUI_password();
                    if (!psd.Equals(newPsd))
                    {
                        Model.UserLogin userLogin = (Model.UserLogin)userLoginDal.getUserByUsername(modUser.getUI_userName());
                        if (null != userLogin)
                        {
                            userLogin.setUL_password(newPsd);
                            userInfoDal.Update(modUser);
                            modifier = userLoginDal.Update(userLogin);
                        }
                    } else {
                        modifier = userInfoDal.Update(modUser);
                         
                    }                   
                }
            

            return modifier;
        }
    }
}
